﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.Data;
using System.Windows.Forms;


namespace UIT_CV_Management
{
    public class NghienCuu
    {
        //ID
        public String maNghienCuu;
        public String maDiaDiem;

        //Properties
        DateTime ngayBatDau;
        DateTime ngayKetThuc;
        String noiDungNghienCuu;
        String tenLuanAn;
        DateTime ngayBaoVe;
        String noiBaoVe;
        String nguoiHuongDan;
        float ketQua;

        // object
        DiaDiemNghienCuu diaDiemNghienCuu
        {
            get
            {
                return new DiaDiemNghienCuu(maDiaDiem);
            }
            set
            {
                diaDiemNghienCuu = value;
            }
        }

        //database
        Database myDatabase = Database.shareDatabase;

        public NghienCuu(DateTime _ngayBatDau, DateTime _ngayKetThuc, String _noiDungNghienCuu, String _tenLuanAn, DateTime _ngayBaoVe, String _noiBaoVe, String _nguoiHuongDan, float _ketQua,DiaDiemNghienCuu _diemDiemNghienCuu)
        {
            //ID
            maNghienCuu = this.getIDFromDatabase();
            maDiaDiem = _diemDiemNghienCuu.maDiaDiem;

            //Properties
            ngayBatDau = _ngayBatDau;
            ngayKetThuc = _ngayKetThuc;
            noiDungNghienCuu = _noiDungNghienCuu;
            tenLuanAn = _tenLuanAn;
            ngayBaoVe = _ngayBaoVe;
            noiBaoVe = _noiBaoVe;
            nguoiHuongDan = _nguoiHuongDan;
            ketQua = _ketQua;

            // object
            diaDiemNghienCuu = _diemDiemNghienCuu;

            
        }
        public NghienCuu(String _maNghienCuu)
        {
            QueryResult result = myDatabase.Query("SELECT * FROM NghienCuu WHERE MaNghienCuu = '" + _maNghienCuu + "'");
            if (result.ReturnCode == 200)
            {
                DataTable table = result.Table;

                //ID
                maNghienCuu = table.Rows[0]["MaNghienCuu"].ToString();
                maDiaDiem = table.Rows[0]["MaDiaDiem"].ToString();

                //Properties
                ngayBatDau = table.Rows[0].Field<DateTime>("NgayBatDau");
                ngayKetThuc = table.Rows[0].Field<DateTime>("NgayKetThuc");
                noiDungNghienCuu = table.Rows[0]["NoiDungNghienCuu"].ToString(); 
                tenLuanAn = table.Rows[0]["TenLuanAn"].ToString();
                ngayBaoVe = table.Rows[0].Field<DateTime>("NgayBaoVe");
                noiBaoVe = table.Rows[0]["NoiBaoVe"].ToString(); 
                nguoiHuongDan = table.Rows[0]["NguoiHuongDan"].ToString(); 
                ketQua = float.Parse(table.Rows[0]["KetQua"].ToString()); 

            }
        }
        private String getIDFromDatabase()
        {
            String x;
            x = Database.getID("NghienCuu", "MaNghienCuu");
            return x;
        }
        public void save()
        {
            String sqlNghienCuuSinh = "INSERT INTO NghienCuu VALUES('" + maNghienCuu + "','" + ngayBatDau.Date.ToString("yyyy-MM-dd HH:mm:ss") + "','" + ngayKetThuc.Date.ToString("yyyy-MM-dd HH:mm:ss") + "','" + maDiaDiem + "','" + noiDungNghienCuu + "','" + tenLuanAn + "','" + ngayBaoVe.Date.ToString("yyyy-MM-dd HH:mm:ss") + "','" + noiBaoVe + "','" + nguoiHuongDan + "'," + ketQua + ")";
            QueryResult result = myDatabase.Query(sqlNghienCuuSinh);

            if (result.ReturnCode == 409)
            {
                MessageBox.Show("Lỗi khi lưu dữ liệu Nghề Nghiệp vào database", "Lỗi", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            else
            {
                diaDiemNghienCuu.save();
            }
        }
        public void update()
        {
            bool isDupplicated = Database.isDupplicate("NghienCuu", "MaNghienCuu", maNghienCuu);

            // update
            if (isDupplicated)
            {
                String sqlUpdate = "UPDATE NghienCuu SET NgayBatDau = '" + ngayBatDau.Date.ToString("yyyy-MM-dd HH:mm:ss") + "', NgayKetThuc = '" + ngayKetThuc.Date.ToString("yyyy-MM-dd HH:mm:ss") + "', NoiDungNghienCuu = '" + noiDungNghienCuu + "', TenLuanAn = '" + tenLuanAn + "', NgayBaoVe = '" + ngayBaoVe.Date.ToString("yyyy-MM-dd HH:mm:ss") + "',NoiBaoVe = '" + noiBaoVe + "',NguoiHuongDan = '" + nguoiHuongDan + "',KetQua = " + ketQua
                    + " WHERE MaNghienCuu = '" + maNghienCuu + "'";
                QueryResult result = myDatabase.Query(sqlUpdate);

                if (result.ReturnCode == 409)
                    MessageBox.Show("Lỗi khi update liệu Thông Tin Phụ vào database", "Lỗi", MessageBoxButtons.OK, MessageBoxIcon.Error);
                else
                {

                }

            }
        }
        public void delete()
        {
            String sqlDelete = "DELETE FROM NghienCuu WHERE MaNghienCuu = '" + maNghienCuu + "'";
            QueryResult result = myDatabase.Query(sqlDelete);

            if (result.ReturnCode == 409)
                MessageBox.Show("Lỗi xoá dữ liệu trong database.", "Lỗi", MessageBoxButtons.OK, MessageBoxIcon.Error);
            else
            {
                diaDiemNghienCuu.delete();
            }
        }
    }
}
